package com.example.boxueguapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNav;
    private ViewPager2 viewPager2;
    private int itemSize;
    private long exitTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomNav = (BottomNavigationView) findViewById(R.id.bottom_nav);
        itemSize = bottomNav.getMenu().size();
        viewPager2 = findViewById(R.id.main_content);
        viewPager2.setUserInputEnabled(false);
        viewPager2.setAdapter(new MyPager2Adapter(this));
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                bottomNav.getMenu().getItem(position).setChecked(true);
            }
        });
        bottomNav.setOnNavigationItemSelectedListener(menuItem -> {
            switch (menuItem.getItemId()) {
                case R.id.shipin:
                    viewPager2.setCurrentItem(0, false);
                    break;
                case R.id.xiti:
                    viewPager2.setCurrentItem(1, false);
                    break;
                case R.id.kecheng:
                    viewPager2.setCurrentItem(2, false);
                    break;
            }
            return true;
        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            if (System.currentTimeMillis()-exitTime>2000){
                Toast.makeText(this, "双击退出", Toast.LENGTH_SHORT).show();
                exitTime=System.currentTimeMillis();
            }else {
                return super.onKeyDown(keyCode, event);
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    class MyPager2Adapter extends FragmentStateAdapter {

        private final Fragment[] fragments;

        public MyPager2Adapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
            fragments = new Fragment[itemSize];
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            if (fragments[position]==null){
                switch (position){
                    case 0:
                        fragments[position]=new ShipinFragment();
                        break;
                    case 1:
                        fragments[position]=new XitiFragment();
                        break;
                    case 2:
                        fragments[position]=new MyFragment();
                        break;
                }
            }
            return fragments[position];
        }

        @Override
        public int getItemCount() {
            return fragments.length;
        }
    }
}